//
//  AttentionVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/13.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "AttentionVC.h"
#import "XYuser.h"
#import "AttentionDetailVC.h"

@interface AttentionVC ()
{
    int pIntPage;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation AttentionVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.pTableView.mj_header beginRefreshing];
}

- (void)initInfo
{
    [super initInfo];
    _pDataDicArr = [NSMutableArray array];
    pIntPage = 1;

    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"关注"];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      
        pIntPage = 1;
        [weakSelf requestDataDicDown];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_footer setHidden:YES];
    [self.pTableView setTableFooterView:[UIView new]];
}
- (void)requestDataDicDown
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestCollectionList:SocialLikeListIP page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [weakSelf.pDataDicArr removeAllObjects];
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"list"];
                       for (NSDictionary *dic in arr)
            {
                XYuser *xyuser = [[XYuser alloc] init];
                
                xyuser.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                xyuser.beReportedCount  = [NSString stringWithFormat:@"%@", [dic valueForKey:@"beReportedCount"]];
                xyuser.breakCount       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                xyuser.distance         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"distance"]];
                xyuser.faceScore        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                xyuser.fans             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                xyuser.headImage        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                xyuser.movieLabel       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                xyuser.nickname         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                xyuser.phone            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                xyuser.pwd              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                xyuser.regTime          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"regTime"]];
                xyuser.saltCode         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"saltCode"]];
                xyuser.uid              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                xyuser.userAge          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                xyuser.userSex          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                xyuser.zambia           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                xyuser.address          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                xyuser.albumNum         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"albumNum"]];
                xyuser.charmValue       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"charmValue"]];
                xyuser.commentCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                xyuser.commentStart     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                xyuser.commentTotalScore= [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                xyuser.type             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                xyuser.activeTime       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                [weakSelf.pDataDicArr addObject:xyuser];
            }
            
            [weakSelf.pTableView reloadData];
    
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
    }];
}

- (void)requestDataDic
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestCollectionList:SocialLikeListIP page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"list"];
            for (NSDictionary *dic in arr)
            {
                XYuser *xyuser = [[XYuser alloc] init];
                
                xyuser.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                xyuser.beReportedCount  = [NSString stringWithFormat:@"%@", [dic valueForKey:@"beReportedCount"]];
                xyuser.breakCount       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                xyuser.distance         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"distance"]];
                xyuser.faceScore        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                xyuser.fans             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                xyuser.headImage        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                xyuser.movieLabel       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                xyuser.nickname         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                xyuser.phone            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                xyuser.pwd              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                xyuser.regTime          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"regTime"]];
                xyuser.saltCode         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"saltCode"]];
                xyuser.uid              = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                xyuser.userAge          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                xyuser.userSex          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                xyuser.zambia           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                xyuser.address          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                xyuser.albumNum         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"albumNum"]];
                xyuser.charmValue       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"charmValue"]];
                xyuser.commentCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                xyuser.commentStart     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                xyuser.commentTotalScore= [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                xyuser.type             = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                xyuser.activeTime       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                
                [weakSelf.pDataDicArr addObject:xyuser];
            }
            
            [weakSelf.pTableView reloadData];
            
            
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }
    }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pDataDicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfAttentionCell"];
   XYuser *user = [_pDataDicArr objectAtIndex:indexPath.row];
 
    UIImageView *imgHead = (UIImageView *)[cell.contentView viewWithTag:1301];
    UIImageView *imgSex = (UIImageView *)[cell.contentView viewWithTag:1302];
    UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblAge = (UILabel *)[cell.contentView viewWithTag:1102];
    UILabel *lblTime = (UILabel *)[cell.contentView viewWithTag:1103];
    UILabel *lblAddress = (UILabel *)[cell.contentView viewWithTag:1104];
    
    UIButton *btnCancel = (UIButton *)[cell.contentView viewWithTag:1323];
    UIButton *btnZan = (UIButton *)[cell.contentView viewWithTag:1322];


    [imgHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/api/common/file/imgGet?fid=%@", IP, user.headImage]]];
    [imgHead.layer setCornerRadius:imgHead.frame.size.width / 2];
    [imgHead.layer setMasksToBounds:YES];
    [lblName setText:user.nickname];
    [lblTime setText:user.activeTimeLast];
    [lblAddress setText:user.address];
    [lblAge setText:user.userAge];
        if ([user.userSex isEqualToString:@"1"]) {
        [imgSex setImage:[UIImage imageNamed:@"sex_boy"]];
    } else {
        [imgSex setImage:[UIImage imageNamed:@"sex_girl"]];
    }
    
    [btnZan setAccessibilityIdentifier:user.uid];
    [btnZan addTarget:self action:@selector(btnZanAction:) forControlEvents:UIControlEventTouchUpInside];
    [btnCancel setAccessibilityIdentifier:user.uid];
    [btnCancel addTarget:self action:@selector(btnCancelAtteniconAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XYuser *user = [_pDataDicArr objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"idfAttentionDetailVC" sender:user];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)btnCancelAtteniconAction:(UIButton *)sender
{
    [SVProgressHUD show];
    [TDNetwork requestDeleteCollection:deleteCollection_interface toUid:sender.accessibilityIdentifier complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [SVProgressHUD showImage:[UIImage imageNamed:@"Attention_cancel"] status:@"已取消关注" maskType:SVProgressHUDMaskTypeBlack];
            [self requestDataDicDown];
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)btnZanAction:(UIButton *)sender
{
    [SVProgressHUD show];
    [TDNetwork requestZambia:zambia_interface toUid:sender.accessibilityIdentifier complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            int intCode = [[dictionary objectForKey:@"ret"] intValue];
            switch (intCode) {
                case 1:
                    [SVProgressHUD showSuccessWithStatus:@"赞成功"];
                    break;

                case 2:
                    //2、	可赞数目为0
                    [SVProgressHUD showWithStatus:@"可赞数目为0"];
                    break;

                case 3:
                    //3、	一天之内，不能对一个人重复赞
                    [SVProgressHUD showWithStatus:@"一天之内，不能对一个人重复赞"];
                    break;

                default:
                    [SVProgressHUD dismiss];
                    break;
            }
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfAttentionDetailVC"]) {
        AttentionDetailVC *nextVC = segue.destinationViewController;
        nextVC.pUser = sender;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
